const app = require("express")();
const bodyParser = require("body-parser");
const axios = require("axios");

app.use(bodyParser.json());
app.use(
  bodyParser.urlencoded({
    extended: false,
  }),
);

// 配置跨域
app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Credentials", true)
  // 允许的域 只有一个则可以使用cookie
  // 若为 * 多个 浏览器出于安全考虑 不能使用cookie
  res.header("Access-Control-Allow-Headers", "Content-Type");
  // res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS')
  // res.setHeader('Content-Type', 'application/json')
  // res.header("Content-Type", "application/json;charset=utf-8")
  next();
});

const port = 8989;
app.listen(port, () => {
  console.log(`http://localhost:${port}`);
});

app.get("/bing", async (req, res) => {
  let request = await axios.get("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=8");
  res.send(request.data);
});

app.post("/leetcode", async (req, res) => {
  let request = await axios.post("https://leetcode-cn.com/graphql", req.body);
  res.send(request.data);
});
